package com.module.login
{
	import com.Connect;
	import com.config.GameConfig;
	import com.connect.agreement.mclgc.Packet;
	import com.game.Global;
	import com.game.Group;
	import com.game.commands.RegRoleCommand;
	
	import flash.utils.ByteArray;
	
	import mx.controls.Alert;
	import mx.utils.ObjectProxy;
	
	import org.phprpc.util.PHPSerializer;
	import org.puremvc.as3.patterns.facade.Facade;

	public class LoginManager
	{
		public static var LoginNormal:int = 1;
		public static var IsLogin:int = 2;
		public static var LogoutNormal:int = 3;
		public static var LoginSuccess:int = 4;
		public static var LoginFailed:int = 5;
		
		public static var instance:LoginManager;
		
		private var _panel:Object;
		
		public function LoginManager(panel:Object)
		{
			instance = this;
			_panel = panel;
		}
		
		public function get panel():Object
		{
			return _panel;
		}

		public function login(account:String,passwd:String):void
		{
			var li:LoginInfo = new LoginInfo();
			li.account = account;
			li.passwd = passwd;
			var p:Packet = new Packet(true,Group.Login,LoginNormal,PHPSerializer.serialize(li));
			Connect.server.sendPackage(p);
		}
		
		public function update(packet:Packet):void
		{
			if(packet==null)
				return;
			if(packet.group != Group.Login)
				return;
			switch(packet.cmd)
			{
				case LoginSuccess:
					Global.user = new ObjectProxy(PHPSerializer.unserialize(packet.data));
					this._panel.visible = false;
					Facade.getInstance().sendNotification(RegRoleCommand.NAME);
					GameConfig.instance.saveAccount = _panel.accountInput.text;
					break;
				case LoginFailed:
					Alert.show("登录失败");
					break;
			}
		}
	}
}